package h0815;

import java.util.LinkedList;
import java.util.Scanner;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 可以组成网络的服务器
 * @date 2024/8/15 17:18
 */
public class ServerMakeNetwork {

    static int n;
    static int m;
    static int[][] matrix;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        m = sc.nextInt();

        matrix = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                matrix[i][j] = sc.nextInt();
            }
        }

        System.out.println(getResult());
    }

    public static int getResult() {
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (matrix[i][j] == 1) {
                    //TODO 从当前节点出发做bfs，更新ans值，取最大
                    ans = Math.max(ans, bfs(i, j));
                }
            }
        }

        return ans;
    }

    static int[][] offsets = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

    public static int bfs(int i, int j) {
        //这里使用bfs，只是把队列中的元素换成了数组int[]来记录下标
        LinkedList<int[]> queue = new LinkedList<>();
        int count = 0; //初始化
        matrix[i][j] = 0; //置为0
        queue.addLast(new int[]{i, j}); //存储坐标
        while (queue.size() != 0) {
            int size = queue.size();
            for (int k = 0; k < size; k++) {
                int[] current = queue.removeFirst();
                for (int l = 0; l < offsets.length; l++) {
                    int newI = current[0] + offsets[l][0];
                    int newJ = current[1] + offsets[l][1];
                    if (newI >= 0 && newI < n && newJ >= 0 && newJ < m && matrix[newI][newJ] == 1) {
                        queue.add(new int[]{newI, newJ});
                        matrix[newI][newJ]=0; //标记已经遍历过。
                    }
                }
                //每poll出去一个，计数器加一
                count++;
            }
        }
        return count;
    }
}
